package graphics.screens;

import javax.microedition.lcdui.Canvas;
import javax.microedition.lcdui.Command;
import javax.microedition.lcdui.CommandListener;
import javax.microedition.lcdui.Displayable;
import javax.microedition.lcdui.Graphics;

import tool.KeyCode;
import tool.ScreenUtils;
import eval.MP3Eval;
import function.Mp3File;
import function.search.EventListener;
import graphics.BootColor;
import graphics.MultiLineText;

public class EvaluationScreen extends Canvas implements CommandListener {

	private static final int X_OFFSET = 10;
	private static final int Y_OFFSET = 10;

	private final Command cancelCommand = new Command("Cancel", Command.EXIT, 1);
	private final Command saveCommand = new Command("Save", Command.OK, 1);
	
	private EventListener[] listeners;

	private MP3Eval mp3eval;
	private boolean isShowTitle;
	private Mp3File mp3;

	private EvaluationScreen() {
		listeners = new EventListener[10];
		addCommand(cancelCommand);
		setCommandListener(this);
	}

	public EvaluationScreen(Mp3File aMp3) {
		this();
		mp3 = aMp3;
		mp3eval = new MP3Eval(aMp3);
		isShowTitle = false;
	}

	protected void paint(Graphics g) {

		ScreenUtils.setBlackBackground(g);
		g.setColor(BootColor.WHITE);
		
		if (isShowTitle) {
			MultiLineText title = new MultiLineText(100, g);
			title.setContent(mp3.getTitle());
			title.repaint(g, BootColor.WHITE);
		} else {			
			drawLine(g, mp3eval.getIsPopular(), "Popular", 1);
			drawLine(g, mp3eval.getAge(), "Age", 2);
			drawLine(g, mp3eval.getTempo(), "Tempo", 3);
			drawLine(g, mp3eval.getSocial(), "Social", 4);
			drawLine(g, mp3eval.getMood(), "Mood", 5);
			drawLine(g, mp3eval.getType(), "Type", 6);
			drawLine(g, mp3eval.getWakee(), "Wakee", 7);
		}

	}

	private void drawLine(Graphics g, String currentValue, String title,
			int rawIndex) {
		int fHeight = g.getFont().getHeight();
		int index = rawIndex;
		g.setColor(BootColor.WHITE);
		String titleWithNum = rawIndex + " - " + title;
		g.drawString(titleWithNum, X_OFFSET, Y_OFFSET + fHeight * index,
				Graphics.LEFT | Graphics.TOP);
		drawValue(X_OFFSET + g.getFont().stringWidth(titleWithNum + " "),
				Y_OFFSET + fHeight * index, currentValue, g);
	}

	private void drawValue(int xPos, int yPos, String value, Graphics g) {
		int color = BootColor.FLASHY_MENU_COLOR;
		if (MP3Eval.NO_VALUE.equals(value)) {
			color = BootColor.GREY;
		}
		g.setColor(color);
		g.drawString(value, xPos, yPos, Graphics.LEFT | Graphics.TOP);
	}

	public void commandAction(Command command, Displayable dis) {
		if (command == cancelCommand) {
//			boot.readyToGo();
			for (int i = 0; (listeners[i] != null) && (i < listeners.length); i++) {
				listeners[i].processEvent(EventListener.EVENT_PROCESS_CANCELED);
			}
		} else if (command == saveCommand) {
			mp3eval.saveToFile();
//			boot.readyToGo();
			for (int i = 0; (listeners[i] != null) && (i < listeners.length); i++) {
				listeners[i].processEvent(EventListener.EVENT_PROCESS_DONE);
			}
		}
	}

	public void keyPressed(int keyCode) {
		if (keyCode == KeyCode.ONE) {
			mp3eval.rotateIsPopular();
		} else if (keyCode == KeyCode.TWO) {
			mp3eval.rotateAge();
		} else if (keyCode == KeyCode.THREE) {
			mp3eval.rotateTempo();
		} else if (keyCode == KeyCode.FOUR) {
			mp3eval.rotateSocial();
		} else if (keyCode == KeyCode.FIVE) {
			mp3eval.rotateMood();
		} else if (keyCode == KeyCode.SIX) {
			mp3eval.rotateType();
		} else if (keyCode == KeyCode.SEVEN) {
			mp3eval.rotateWakee();
		} else if (keyCode == KeyCode.DIESE) {
			isShowTitle = !isShowTitle;
		}
		repaint();

		if (mp3eval.areAllValuesSet()) {
			addCommand(saveCommand);
		}
	}


	public void addListener(EventListener listener) {
		int i = 0;
		while ((listeners[i] != null) && (i < listeners.length)) {
			i++;
		}
		listeners[i] = listener;
	}
}
